package com.mock.water.modules.system.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mock.water.config.shiro.JwtToken;
import com.mock.water.core.utils.AjaxResult;
import com.mock.water.core.utils.ConvertUtil;
import com.mock.water.core.utils.JwtUtils;
import com.mock.water.modules.system.user.entity.UserEntity;
import com.mock.water.modules.system.user.mapper.UserMapper;
import com.mock.water.modules.system.user.model.UserDto;
import com.mock.water.modules.system.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author dell
 * @description 针对表【sys_user】的数据库操作Service实现
 * @createDate 2022-10-13 16:45:15
 */
@CacheConfig(cacheNames = {"cacheOne"})
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {



    /**
     * 查询
     * 缓存注解的方法的返回结果
     * condition表示的是条件（为true才缓存）
     */
    @Cacheable(key = "#username", condition = "#username.length==5")
    @Override
    public UserEntity queryUserByUsername(String username) {
        System.out.println(username);

        UserEntity user = baseMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUsername, username));

        System.out.println("useCache查询");
        return user;
    }

    /**
     * 查询所有
     * cacheNames: 指令缓存名称
     * cacheNames + keyGenerator
     */
    @Cacheable(cacheNames = "cacheTwo", keyGenerator = "keyGenerator")
    @Override
    public List<UserEntity> findAllCache(String cacheKey) {
        System.out.println(cacheKey);

        List<UserEntity> allUser = baseMapper.selectList(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUsername, "admin"));

        return allUser;
    }
    /**
     * 更新
     * condition表示的是条件（为true才缓存）
     */
    @CachePut( key = "#cacheKey", condition = "#cacheKey=='token'")
    @Override
    public UserEntity updateCache(String cacheKey) {

        Optional<UserEntity> optional = Optional.ofNullable(baseMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUsername, "admin")));

        if(!optional.isPresent()){
            return null;
        }

        UserEntity user = optional.get();
        user.setNickname(cacheKey);
        baseMapper.updateById(user);

        return user;
    }

    /**
     * 更加id，删除员工
     *
     * @CacheEvict Spring会在调用该方法之前清除缓存中的指定元素
     * allEntries : 为true表示清除value空间名里的所有的数据，默认为false
     * beforeInvocation 缓存的清除是在方法前执行还是方法后执行，默认是为false,方法执行后删除
     * beforeInvocation = false : 方法执行后删除，如果出现异常缓存就不会清除
     * beforeInvocation = true : 方法执行前删除，无论方法是否出现异常，缓存都清除
     */

    @CacheEvict(key = "#cacheKey", beforeInvocation = true)
    @Override
    public boolean deleteCache(String cacheKey) {
        log.info("deleteById查询数据库");
        try {
            baseMapper.deleteById(cacheKey);
            log.info("删除成功");
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}




